package com.fs.dao;

import com.fs.bean.MonitorFalconTrend;
import com.fs.domain.MonitorFalcon;
import com.fs.util.SqlUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SqlBuilder;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by fengshuai on 17/5/18.
 */
@Repository
public interface MonitorFalconDao {
    public static final String TABLE = "monitor_falcon";
    public static final String INSERT_FIELDS = "`day`,db_id,segment,ts,qps,machine,ctime,utime,valid";
    public static final String INSERT_VALUES = "#{day},#{db_id},#{segment},#{ts},#{qps},#{machine},#{ctime},#{utime},#{valid}";
    public static final String SELECT_FIELDS = "id," + INSERT_FIELDS;

    @Select("select " + SELECT_FIELDS + " from " + TABLE + " where valid=1 and day=#{day}")
    public List<MonitorFalcon> batchGetByDay(@Param("day") int day);

    @SelectProvider(type=SQLProvider.class,method="batchGetByDynamicParam")
    public List<MonitorFalcon> batchGetByDynamicParam(@Param("day") int day, @Param("dbId") int dbId, @Param("segment") int segment);

    @SelectProvider(type = SQLProvider.class, method = "batchGetMaxQpsByDayAndDbIds")
    public List<Long> batchGetLatestIdsByDayAndDbIds(@Param("startDay") int startDay, @Param("endDay") int endDay, @Param("dbIds") List<Integer> dbIds, @Param("segment") int segment);

    @SelectProvider(type = SQLProvider.class, method = "batchGetByIds")
    public List<MonitorFalconTrend> batchGetByIds(@Param("ids") List<Long> ids);

    static class SQLProvider{
        public String batchGetByDynamicParam(Map<String, Object> params) {
            SqlBuilder.BEGIN();
            SqlBuilder.SELECT(SELECT_FIELDS);
            SqlBuilder.FROM(TABLE);
            SqlBuilder.WHERE("valid=1");
            SqlBuilder.WHERE("day=#{day}");
            if (params.get("dbId") != null && !params.get("dbId").equals(0)) {
                SqlBuilder.WHERE("db_id=#{dbId}");
            }
            if (params.get("segment") != null && !params.get("segment").equals(0)) {
                SqlBuilder.WHERE("segment=#{segment}");
            }
            SqlBuilder.ORDER_BY("ctime asc");
            return SqlBuilder.SQL();
        }

        public String batchGetMaxQpsByDayAndDbIds(Map<String, Object> params) {
            SqlBuilder.BEGIN();
            SqlBuilder.SELECT("max(id) as id");
            SqlBuilder.FROM(TABLE);
            SqlBuilder.WHERE("valid=1");
            SqlBuilder.WHERE("segment=#{segment}");
            SqlBuilder.WHERE(SqlUtil.preparedInSql(params, "dbIds", "db_id", true));
            SqlBuilder.WHERE("`day` >= #{startDay}");
            SqlBuilder.WHERE("`day` <= #{endDay}");
            SqlBuilder.GROUP_BY("`day`,db_id,segment");
            return SqlBuilder.SQL();
        }

        public String batchGetByIds(Map<String, Object> params) {
            SqlBuilder.BEGIN();
            SqlBuilder.SELECT(SELECT_FIELDS);
            SqlBuilder.FROM(TABLE);
            SqlBuilder.WHERE("valid=1");
            SqlBuilder.WHERE(SqlUtil.preparedInSql(params, "ids", "id", true));
            return SqlBuilder.SQL();
        }
    }
}
